package REMC.objects;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * @authors Tal Cohen & Adiel Ashrov
 */
public class MyLogger {

	private static MyLogger instance = null;
	private static Logger log = null;
	private static FileHandler hand = null;
	private static myFormatter formatter = null;

	private MyLogger(){

		try {
			hand = new FileHandler("adielLog.txt");
			formatter = new myFormatter();
			hand.setFormatter(formatter);

			log = Logger.getLogger("log_file");
			log.setLevel(Level.INFO);
			log.addHandler(hand);
			log.setUseParentHandlers(false);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static synchronized MyLogger getLogger(){
		if (instance == null){
			instance = new MyLogger();
		}
		return instance;
	}

	public synchronized void Log(String msg){
		System.out.println(msg);
		log.info(msg);
	}

	public synchronized void close(){
		hand.close();
	}

	private class myFormatter extends SimpleFormatter{
		@Override
		public String format(LogRecord record){
			if(record.getLevel() == Level.INFO){
				return record.getMessage() + "\r\n";
			}else{
				return super.format(record);
			}
		}

	}


}
